<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '240px'" class="layout-aside">
      <div class="logo" :class="{ 'logo-collapsed': isCollapse }">
        <img src="@/assets/logo.svg" alt="Logo" />
        <span v-show="!isCollapse">人力资源管理系统</span>
        <span v-show="isCollapse" class="logo-mini">HR</span>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :collapse="isCollapse"
        :collapse-transition="false"
        background-color="transparent"
        text-color="rgba(255, 255, 255, 0.8)"
        active-text-color="#409EFF"
        router
      >
        <template v-for="menu in menuRoutes" :key="menu.path">
          <el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.path">
            <template #title>
              <el-icon><component :is="menu.meta?.icon || Setting" /></el-icon>
              <span>{{ menu.meta?.title }}</span>
            </template>
            <el-menu-item v-for="child in menu.children" :key="child.path" :index="child.path">
              <el-icon><component :is="child.meta?.icon || Setting" /></el-icon>
              <span>{{ child.meta?.title }}</span>
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item v-else :index="menu.path">
            <el-icon><component :is="menu.meta?.icon || Setting" /></el-icon>
            <span>{{ menu.meta?.title }}</span>
          </el-menu-item>
        </template>
      </el-menu>
      
      <!-- 侧边栏底部操作 -->
      <div class="sidebar-footer">
        <el-tooltip :content="isCollapse ? '展开菜单' : '折叠菜单'" placement="right">
          <el-button 
            type="text" 
            class="collapse-btn" 
            @click="toggleSidebar"
            :icon="isCollapse ? Expand : Fold"
          />
        </el-tooltip>
      </div>
    </el-aside>
    
    <!-- 主内容区域 -->
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="layout-header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">
              <el-icon><House /></el-icon>
              首页
            </el-breadcrumb-item>
            <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="index">
              {{ item.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <!-- 用户信息 -->
          <el-dropdown trigger="click" @command="handleCommand" class="header-action">
            <div class="user-profile">
              <el-avatar :size="36" :src="userInfo?.avatar || defaultAvatar" />
              <div class="user-info">
                <div class="user-name">{{ userInfo?.realName }}</div>
                <div class="user-role">{{ userInfo?.position || '管理员' }}</div>
              </div>
              <el-icon><CaretBottom /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu class="user-dropdown">
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人信息
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  系统设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 主内容区域 -->
      <el-main class="layout-main">
        <router-view v-slot="{ Component }">
          <transition name="fade-slide" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Setting, 
  Fold, 
  Expand, 
  CaretBottom, 
  Calendar,
  House,
  Bell,
  User,
  SwitchButton
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'
import router from '@/router'

const route = useRoute()
const routerInstance = useRouter()
const authStore = useAuthStore()

const isCollapse = ref(false)
const userInfo = computed(() => authStore.userInfo)
const defaultAvatar = ''

const activeMenu = computed(() => route.path)

const menuRoutes = computed(() => {
  const layoutRoute = router.options.routes.find(r => r.name === 'Layout')
  if (!layoutRoute || !layoutRoute.children) return []
  
  return layoutRoute.children
    .filter(route => route.meta?.title && route.meta?.title !== '首页')
    .map(route => ({
      path: route.path,
      meta: route.meta,
      children: route.children?.map(child => ({
        path: child.path,
        meta: child.meta
      })) || []
    }))
})



const breadcrumbs = computed(() => {
  return route.matched
    .filter(item => item.meta?.title)
    .map(item => ({
      title: item.meta?.title,
      path: item.path
    }))
})

function toggleSidebar() {
  isCollapse.value = !isCollapse.value
}

function handleCommand(command: string) {
  switch (command) {
    case 'profile':
      ElMessage.info('个人信息功能开发中')
      break
    case 'settings':
      ElMessage.info('系统设置功能开发中')
      break
    case 'logout':
      authStore.logout()
      routerInstance.push('/login')
      break
  }
}
</script>

<style scoped>
/* 布局容器 */
.layout-container {
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7ed 100%);
}

/* 侧边栏样式 */
.layout-aside {
  background: linear-gradient(180deg, #2c3e50 0%, #1a2530 100%);
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
  position: relative;
  overflow: hidden;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.05);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  transition: all 0.3s ease;
  padding: 0 16px;
}

.logo-collapsed {
  justify-content: center;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
  transition: margin 0.3s ease;
}

.logo-collapsed img {
  margin-right: 0;
}

.logo-mini {
  font-size: 14px;
  font-weight: bold;
  color: #409EFF;
}

.el-menu-vertical {
  border-right: none;
  height: calc(100vh - 120px);
  background: transparent !important;
}

.el-menu-vertical .el-menu-item,
.el-menu-vertical .el-sub-menu__title {
  height: 48px;
  line-height: 48px;
  margin: 4px 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-menu-vertical .el-menu-item:hover,
.el-menu-vertical .el-sub-menu__title:hover {
  background: rgba(255, 255, 255, 0.1) !important;
  transform: translateX(2px);
}

.el-menu-vertical .el-menu-item.is-active {
  background: linear-gradient(90deg, #409EFF 0%, #53a8ff 100%) !important;
  color: #fff !important;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

/* 侧边栏底部 */
.sidebar-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
}

.collapse-btn {
  width: 100%;
  color: rgba(255, 255, 255, 0.7) !important;
  transition: all 0.3s ease;
}

.collapse-btn:hover {
  color: #fff !important;
  transform: scale(1.05);
}

/* 顶部导航栏 */
.layout-header {
  background: linear-gradient(90deg, #ffffff 0%, #f8fafc 100%);
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  height: 64px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-left .el-breadcrumb {
  font-size: 14px;
}

.header-left .el-breadcrumb :deep(.el-breadcrumb__inner) {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #64748b;
  transition: color 0.3s ease;
}

.header-left .el-breadcrumb :deep(.el-breadcrumb__inner:hover) {
  color: #409EFF;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-action {
  transition: all 0.3s ease;
}

.header-action:hover {
  transform: translateY(-1px);
}

.badge-notification :deep(.el-badge__content) {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(255, 107, 107, 0.3);
}

/* 用户信息 */
.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  border-radius: 12px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  min-width: 180px;
}

.user-profile:hover {
  background: #ffffff;
  border-color: #cbd5e1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-1px);
}

.user-info {
  flex: 1;
  min-width: 0;
}

.user-name {
  font-size: 14px;
  font-weight: 600;
  color: #1e293b;
  line-height: 1.2;
  margin-bottom: 2px;
}

.user-role {
  font-size: 12px;
  color: #64748b;
  line-height: 1;
}

/* 下拉菜单 */
.user-dropdown .el-dropdown-menu__item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.user-dropdown .el-dropdown-menu__item:hover {
  background: linear-gradient(90deg, #f1f5f9 0%, #e2e8f0 100%);
  color: #409EFF;
}

/* 主内容区域 */
.layout-main {
  padding: 24px;
  background: #f8fafc;
  overflow: auto;
  min-height: calc(100vh - 64px);
}

/* 动画效果 */
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.fade-slide-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.fade-slide-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .layout-header {
    padding: 0 16px;
  }
  
  .header-right {
    gap: 12px;
  }
  
  .user-profile {
    min-width: auto;
    padding: 6px 12px;
  }
  
  .user-info {
    display: none;
  }
  
  .layout-main {
    padding: 16px;
  }
}

/* 滚动条样式 */
.layout-main::-webkit-scrollbar {
  width: 6px;
}

.layout-main::-webkit-scrollbar-track {
  background: #f1f5f9;
  border-radius: 3px;
}

.layout-main::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 3px;
  transition: background 0.3s ease;
}

.layout-main::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}
</style>